﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine;
using System.Net;
using Microsoft.Xna.Framework;
using Engine.Objects;
using System.Net.NetworkInformation;
using Engine.MarchCube;

namespace ObjectEditor
{
    public unsafe class EditorScene : Engine.Scene.BaseScene
    {
        private NOWAEngine _game;
        Engine.MarchCube.Chunk m_chunk;
        BaseElement workCust;
        float angle = 0;
        public EditorScene(NOWAEngine _game)
            : base(_game, NameScene)
        {

            // TODO: Complete member initialization


            this._game = _game;
            DEFAULTCamera cam = (DEFAULTCamera)(((Engine.NOWAEngine)Game).Manager.GetComponent("CameraManager")).GetMaincamera();
            cam.LookAt(new Vector3(0, 0, 0));
            cam.Position = new Vector3(0, 0, 0);
            cam.ClickAndDragMouseRotation = true;
            cam.OrbitTarget = new Vector3(0, 0, 0);
            cam.CurrentBehavior = BaseCamera.Behavior.Orbit;
        }
        public override void LoadContent()
        {
            Engine.Voxel.Materials.init();
            Engine.Voxel.Materials.LoadContent(Game.Content);
            base.LoadContent();
        }
        public override void Initialize()
        {
            float grid=1f / 4f;
            m_chunk = new Engine.MarchCube.Chunk(_game, "ch1", new Vector3(-16, -16, -16), new Vector3(10f, 10f, 10f), Quaternion.Identity, 10,grid );
            m_chunk.LoadContent();
            ObjControler.Object3D obj = new ObjControler.Object3D("obj1", 0, new Vector3(5, 5, 5), 10);
            ObjControler.Sphere sp1 = new ObjControler.Sphere(new Vector3(5, 5, 5), 2, -1);
            obj.AddElement(sp1);
             int Size =(int) (20 / grid);
             ObjControler.MapObject<float> map = new ObjControler.MapObject<float>(Size * Size * Size);
            long tic=Environment.TickCount;
            for (float x = -10; x < 10; x += grid)
            {
                for (float y = -10; y < 10; y += grid)
                {
                    for (float z = -10; z < 10; z += grid)
                    {
                        int xScal =(int)(x / grid);
                        int yScal = (int)(y / grid);
                        int zScal = (int)(z / grid);
                       
                        int index = xScal * Size * Size + yScal * Size + zScal;
                        map[index] = obj.IntersectObject(x, y, z);
                    }
                }
            }
            tic = Environment.TickCount - tic;
            base.Initialize();

            //workCust = m_chunk.CreateElement("fristT", Vector3.Zero);
            //workCust[5, 5, 5] = 1;
            //workCust[5, 5, 7] = 1;
            //workCust[5, 7, 7] = 1;
            //workCust[5, 7, 5] = 1;
            //workCust[7, 5, 5] = 1;
            //workCust[7, 5, 7] = 1;
            //workCust[7, 7, 7] = 1;
            //workCust[7, 7, 5] = 1;

            //  workCust = m_chunk.CreateElement("fristT1",new Vector3(5,5,5));
            //   workCust.mat = Engine.Voxel.Materials.GetMat("Granit");
            //workCust[6, 6, 6] = 2;
            //workCust[5, 6, 6] = 2;
            //workCust[6, 7, 7] = 2;
            //workCust[6, 7, 6] = 2;
            //workCust[6, 5, 6] = 2;
            //workCust[7, 6, 6] = 2;
            //workCust[6, 6, 7] = 2;
            //workCust[6, 6, 5] = 2;            
            //workCust[6, 7, 7] = 2;
            //    workCust[6, 5, 7] = 2;
            //   workCust[7, 7, 6] = 2;


            //   workCust[8, 8, 8] = 3;
            //workCust.GetData.RndDat();
            //workCust = m_chunk.CreateElement("fristT1", new Vector3(10,0,0));
            //workCust.GetData.RndDat();
        }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (angle > 360)
            {
                angle = 0;
            }
            angle += 0.01f;
            //m_chunk.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, angle);
        }
        public static string NameScene { get { return "Edit"; } }
        public static EditorScene ActionCreate(NOWAEngine _game)
        {
            return new EditorScene(_game);
        }
    }
}
